//#include <opkg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/reboot.h>
#include "driver_spinor.h"


int opkgInstallFile(const char *pkgname,const char *ipkfile) {

    // 卸载软件包
    char cmd[128]; 
    sprintf(cmd, "opkg remove %s", pkgname);
    printf("Uninstalling package...\n");
    if (system(cmd) != 0) {
        printf("Failed to uninstall package.\n");
    } else {
        printf("Package uninstalled successfully.\n");
    }

    memset(cmd, 0, sizeof(cmd));
    // 升级软件包
    sprintf(cmd, "opkg install %s --force-overwrite", ipkfile);
    printf("Upgrading package...\n");
    if (system(cmd) != 0) {
        printf("Failed to upgrade package.\n");
        return 1;
    } else {
        printf("Package upgraded successfully.\n");
    }
    
    return 0;
}

void upgradePcs_shell() {
    system("/etc/config/upgrade.sh");
}


int upgradeFpga() {

    if (drv_spinor_write_file("/tmp/fpga.bin") != 0) {
        printf("[updateFPGA] Failed to write FPGA image.\n");
        return -1;
    }
    
    // 等待 FPGA 写入完成
    sleep(30); 

    // 执行系统重启
    if (reboot(RB_AUTOBOOT) != 0) {
        printf("[updateFPGA] Failed to reboot system.\n");
        return -1;
    }
    
    return 0;
}